//以下为修改jQuery Validation插件兼容Bootstrap的方法，没有直接写在插件中是为了便于插件升级
$.validator.setDefaults({
    highlight: function (element) {
        $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
    },
    success: function (element) {
        element.closest('.form-group').removeClass('has-error').addClass('has-success');
    },
    errorElement: "span",
    errorPlacement: function (error, element) {
        if (element.is(":radio") || element.is(":checkbox")) {
            error.appendTo(element.parent().parent().parent());
        } else {
            error.appendTo(element.parent());
        }
    },
    errorClass: "help-block m-b-none",
    validClass: "help-block m-b-none"


});

//以下为官方示例
$().ready(function () {
    // validate the comment form when it is submitted
    // $("#commentForm").validate();

    // validate signup form on keyup and submit
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#regForm").validate({
        rules: {
            'member.username': {
                required: true,
                remote: {
                    url: "/shop/member/checkUsername?shopId=0",
                    type: "get",
                    dataType: "json",
                    data: {
                        'member.username': function () {
                            return $("#username").val();//这个是取要验证的密码
                        }
                    },
                    dataFilter: function (data) {//判断控制器返回的内容
                        if (data == "true") {
                            return true;
                        }else {
                            return false;
                        }
                    }
                }
            },
            'member.password': {
                required: true,
                minlength: 5
            },
            rePassword: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            'member.email': {
                required: true,
                email: true
            }
        },
        messages: {
            'member.username': {
                required: icon + "请输入你的用户名",
                remote: "该用户名已被注册，请重新选择"
            },
            'member.password': {
                required: icon + "请输入您的密码",
                minlength: icon + "密码必须5个字符以上"
            },
            rePassword: {
                required: icon + "请再次输入密码",
                minlength: icon + "密码必须5个字符以上",
                equalTo: icon + "两次输入的密码不一致"
            },
            'member.email': icon + "请输入您的E-mail"
        }
    });
});
